<?php

namespace App\Http\model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class Order extends Model
{

	/**
	 * [等待司机接单时展示信息函数]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @version  0.8
	 * @param    [订单ID] [int] orderId
	 * @param    [查询字段] [array] fields
	 * @return   [json] $result
	 */
	public function waitReceiptModel($fields,$orderId){


		$orderInfo = db::table('order')
					->where('order.id',$orderId)
					->select($fields)
					->join('car_series','order.car_id','=','car_series.id')
					->first();

		if ($orderInfo) {
			$orderInfoNew = json_decode(json_encode($orderInfo), true);
			$orderInfoNew['appointment'] = date('Y-m-d H:i:s',$orderInfoNew['appointment']);
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $orderInfoNew;
			return json_encode($result);
		}else{
			$result['code'] = 1004;
			$result['msg'] = '未查询到数据';
			return json_encode($result);
		}
	}

	/**
	 * [订单进行中函数]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [订单ID] [int] orderId
	 * @param    [查询字段] [array] fields
	 * @version  0.8
	 * @return   [json]  $result
	 */
	public function orderProcessModel($fields,$orderId){

		$orderInfo = db::table('order')
					->where('order.id',$orderId)
					->select($fields)
					->join('car_series','order.car_id','=', 'car_series.id')
					->join('chauffeur','order.chauffeur_id','=', 'chauffeur.id')
					->join('cars','cars.id','=', 'chauffeur.car_id')
					->first();

		if ($orderInfo) {
			$orderInfoNew = json_decode(json_encode($orderInfo), true);
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $orderInfoNew;
			return json_encode($result);
		}else{
			$result['code'] = 1004;
			$result['msg'] = '未查询到数据';
			return json_encode($result);
		}

	}

	/**
	 * [查询需取消的订单信息函数]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [订单ID] [int] orderId
	 * @param    [查询字段] [array] fields
	 * @version  0.8
	 * @return   [json]  $result
	 */
	public function orderInfo($fields,$orderId){

		$orderInfo = db::table('order')
					->where('order.id',$orderId)
					->select($fields)
					->join('social_user','order.user_id','=', 'social_user.id')
					->join('server_item','order.type','=', 'server_item.id')
					->first();

		if ($orderInfo) {
			$orderInfoNew = json_decode(json_encode($orderInfo), true);
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $orderInfoNew;
			return $result;
		}else{
			$result['code'] = 1004;
			$result['msg'] = '订单不存在';
			return $result;
		}
	}

	/**
	 * [取消订单后对预扣款/订单操作函数]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [用户ID] [int] uid
	 * @param    [修改值] [int] money
<<<<<<< HEAD
	 * @param    [订单ID] [int] orderid
	 * @version  0.8
	 * @return   [type]     [description]
	 */
	public function updateModel($money,$uid,$orderid){
=======
	 * @param    [订单ID] [int] orderId
	 * @version  0.8
	 * @return   [type]     [description]
	 */
	public function updateModel($money,$uid,$orderId){
>>>>>>> d2862f3c73f04b75a3d1bcf48bab78511e50bd8b

		$result = array();

		$data['sum_money'] = $money;

		$updateUserRes = db::table('social_user')
				 ->where('id',$uid)
				 ->update($data);

		if ($updateUserRes) {

			$orderData['status'] = 0;
			$updateOrderRes = db::table('order')
<<<<<<< HEAD
					 ->where('id',$orderid)
=======
					 ->where('id',$orderId)
>>>>>>> d2862f3c73f04b75a3d1bcf48bab78511e50bd8b
					 ->update($orderData);

			if ($updateOrderRes) {
				
				$result['code'] = 1000;
				$result['msg'] = '取消成功';
				return $result;

			}else{

				$result['code'] = 1004;
				$result['msg'] = '取消失败';
				return $result;
			}
<<<<<<< HEAD
=======


		}else{
			$result['code'] = 1004;
			$result['msg'] = '取消失败';
			return $result;
		}
	}

	/**
	 * [修改订单状态]
	 * @Author   孙丽男
	 * @DateTime 2018-05-28
	 * @param    [订单ID] [int] orderId
	 * @version  0.8
	 * @return   [array]   $result
	 */
	public function updateStatusModel($orderId){

		$result = array();

		$orderData['status'] = 0;

		$updateOrderRes = db::table('order')
				 ->where('id',$orderId)
				 ->update($orderData);

		if ($updateOrderRes) {
>>>>>>> d2862f3c73f04b75a3d1bcf48bab78511e50bd8b


		}else{

			$result['code'] = 1004;
			$result['msg'] = '取消失败';
			return $result;
		}
	}
}
